package com.wsc.service.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.wsc.entity.Student;
import com.wsc.repository.StudentRepository;
import com.wsc.service.StudentService;

@Service
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentRepository sr;

	@Override
	public Integer add(Student stu) {
		try {
			// 禁止重复卡号添加数据
			List<Student> list = sr.findAll();
			if (list.size() > 0) {
				for (Integer i = 0; i < list.size(); i++) {
					// 与数据库表中的cardno比较是否已存在
					if (stu.getCardno().equals(list.get(i).getCardno())) {
						return 0;
					}
				}
			}
			sr.save(stu);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public Integer deleteById(Integer id) {
		try {
			// 一个id字段，jpa识别不了是student还是membership的id
			Student stu = new Student();
			stu.setId(id);
			sr.delete(stu);
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}

	@Override
	public Integer update(Student stu) {
		try {
			// 禁止修改卡号为已存在的
			List<Student> list = sr.findAll();
			if (list.size() > 0) {
				for (Integer i = 0; i < list.size(); i++) {
					if (stu.getCardno().equals(list.get(i).getCardno())) {
						// id不同，则为两条条数据，cardid唯一的,不允许重复
						if (!stu.getId().equals(list.get(i).getId())) {
							return 0;
						}
						break;
					}
				}
			}
			sr.save(stu);
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}

	@Override
	public Map<String, Object> getAllStu(Map<String, Object> params) {
		Specification<Student> spec = getSpec(params);
		Integer page = (Integer) params.get("page");
		Integer limit = (Integer) params.get("limit");
		Pageable pa = PageRequest.of(page, limit);
		// 调用查询方法
		Page<Student> res = sr.findAll(spec, pa);
		// data
		List<Student> data = res.getContent();
		// count
		long count = res.getTotalElements();
		if (data.size() > 0) {
			params.put("code", 0);
			params.put("data", data);
			params.put("count", count);
		} else {
			params.put("code", 1);
			params.put("msg", "没有数据");
		}
		return params;
	}

	private Specification<Student> getSpec(Map<String, Object> params) {
		return new Specification<Student>() {
			@Override
			public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate pre = cb.conjunction();
				List<Expression<Boolean>> sqllist = pre.getExpressions();
				// id cardno name sex membershipid status remark stuno
				if (!StringUtils.isEmpty(params.get("cardno"))) {
					sqllist.add(cb.like(root.get("cardno"), "%" + params.get("cardno") + "%"));
				}
				if (!StringUtils.isEmpty(params.get("name"))) {
					sqllist.add(cb.like(root.get("name"), "%" + params.get("name") + "%"));
				}
				if (!StringUtils.isEmpty(params.get("membershipid"))) {
					sqllist.add(cb.equal(root.get("membershipid"), (Integer)params.get("membershipid")));
				}
				if (!StringUtils.isEmpty(params.get("stuno"))) {
					sqllist.add(cb.like(root.get("stuno"), "%" + params.get("stuno") + "%"));
				}
				if (!StringUtils.isEmpty(params.get("status"))) {
					sqllist.add(cb.equal(root.get("status"), params.get("status")));
				}
				return pre;
			}
		};
	}

	@Override
	public Student findByCardno(String cardno) {

		return sr.findByCardno(cardno);
	}

	@Override
	public Student getOne(Integer id) {
		return sr.getOne(id);
	}

	@Override
	public void updateAllStatusToZero() {
		sr.updateAllStatusToZero();
	}

}
